package org.NooLab.glue.components;

import java.util.Timer;
import java.util.TimerTask;

import org.NooLab.glue.net.ComBoxIntf;
import org.NooLab.utilities.logging.PrintLog;

public class ConfirmedSender implements Runnable,
										SenderConfirmationIntf {

	String xml;
	int port;
	boolean timedOut, processCompleted;
	Thread regsendingThrd;
	int delayAsTimeOut = 8500;
	int delayPeriodBetween = 2000;
	boolean enforcedStop = false;
	public Timer timer;

	String transactionSecret="";
	String successMsg = "sending has been successful.";
	String failMsg = "sending failed.";
	  
	ComBoxIntf comBox;
	PrintLog out;

	
	public ConfirmedSender( ComBoxIntf combox, String transactionsecret, PrintLog outprn){

		out = outprn;
		comBox = combox;
		transactionSecret = transactionsecret;
	}
	
	public void stopWaiting(){
		
		timer.cancel();
		timer.purge();
	}
	
	public SenderConfirmationIntf start( String xml, int port ) {

		this.xml = xml;
		this.port = port;

		regsendingThrd = new Thread(this, "RegSender");
		

		// replace with IrTimer from utilities?
		timer = new Timer(false);
		timer.schedule( new TimerTask() {
										  public void run() {
											out.print(5, "Requesting stop of registration thread <RegSender>...");
											requestStop();
										  }
										}, 
						delayAsTimeOut);
		
		regsendingThrd.start();
		
		return this;
	}

	@Override
	public void informAboutConfirmation(boolean isConfirmed, String secret) {
		 
		// we should only accept the confirmation,
		// if the "secret" matches "transactionSecret" 
		// not used so far ...
		processCompleted = isConfirmed;

	}

	@SuppressWarnings("static-access")
	public void requestStop() {
		enforcedStop = true;
		try {
			Thread.currentThread().sleep(200);
		} catch (InterruptedException e) {
		}

		try {
			regsendingThrd.join();
		} catch (InterruptedException e) {
		}
	}

	@SuppressWarnings("static-access")
	private void regSendingHandler() {

		int z, r;
		String xmlRestPrefix;
		SenderWrap sender=null; 
		
		out.print(5, "...sending request for registration to MessageBoard...");

		
		processCompleted = false; enforcedStop=false;
		z = 0;
		r = -1;
		while ((r <= 0) && (z < 5) &&  (processCompleted==false)) { // (enforcedStop == false) &&
			r = 0;
			// enforce sending only once, for now -> ONLY DEBUG
			
			sender = new SenderWrap(comBox);
			sender.sendingMessage(xml, port);
			
			delay( delayPeriodBetween ) ;
			z++;
		}
		if (processCompleted==false){
			out.printErr(1, failMsg );
		} else{
			out.print(4, successMsg);
		}
		if (sender!=null){
			sender=null;
		}
	} 

	
	
	@SuppressWarnings("static-access")
	private void delay(int millis){
		try {
				Thread.currentThread().sleep(millis);
		} catch (Exception e) {}
	}
	
	
	@Override
	public void run() {
		regSendingHandler();
	}

	public boolean isProcessCompleted() {
		return processCompleted;
	}

	public void setProcessCompleted(boolean processCompleted) {
		this.processCompleted = processCompleted;
	}

	public void setDelayAsTimeOut(int delayAsTimeOut) {
		this.delayAsTimeOut = delayAsTimeOut;
	}

	public void setDelayPeriodBetween(int delayPeriodBetween) {
		this.delayPeriodBetween = delayPeriodBetween;
	}

	public void setSuccessMsg(String successMsg) {
		this.successMsg = successMsg;
	}

	public void setFailMsg(String failMsg) {
		this.failMsg = failMsg;
	}

}
